from flask import request, jsonify
from user import userservice
from file.oss_service import oss_service

def init_oss_routes(app):
    """
    初始化OSS相关路由
    """
    
    @app.route('/oss/sts', methods=['GET'])
    def get_sts_token():
        """
        获取OSS STS临时凭证
        """
        source = request.args.get('source')
        source_id = request.args.get('source_id')
        user_id = userservice.get_user_by_source_id(source_id, source)
        if user_id is None:
            return jsonify({
                'error': '请先登录',
                'message': '请先登录, 联系管理员'
            }), 400

        try:
            result = oss_service.generate_sts_token(user_id)
            return jsonify(result)
        except Exception as e:
            return jsonify({
                'success': False,
                'error': '获取STS凭证失败',
                'message': str(e)
            }), 500

    @app.route('/oss/signature', methods=['GET'])
    def get_sts_signature():
        """
        获取OSS STS签名
        """
        source = request.args.get('source')
        source_id = request.args.get('source_id')
        user_id = userservice.get_user_by_source_id(source_id, source)
        if user_id is None:
            return jsonify({
                'error': '请先登录',
                'message': '请先登录, 联系管理员'
            }), 400

        try:
            result = oss_service.get_sts_signature(user_id)
            return jsonify(result)
        except Exception as e:
            return jsonify({
                'success': False,
                'error': '获取STS凭证失败',
                'message': str(e)
            }), 500 